/**
 * Created by Anarld on 2017/6/6.
 */
var userDao = require('../mysql/userDao.js');
var robotDao = require('../mysql/robotDao.js');
var msg = require('../util/msg.js');

var gameUser = module.exports;

gameUser.setGameId = function (id, gameId) {
    var userModel = new userDao();
    userModel.inId(id, function (err, res) {
        if (res == null) {
            return false;
        }
        userModel.set('gameId', gameId);
        userModel.save(function (err1, res1) {
        });
    });
};
gameUser.clearGameId = function (id, cid, uuid) {
    var userModel = new userDao();
    userModel.inId(id, function (err, res) {
        if (res == null) {
            return false;
        }
        if (res.res.gameId == 0) {
            return false;
        }
        userModel.set('gameId', 0);
        userModel.save(function (err1, res1) {
            if (cid && uuid) {
                msg.send(cid, uuid, 'gameIdClear');
            }
        });
    });
};
gameUser.setRobot = function (id, gameId) {
    var robotModel = new robotDao();
    robotModel.inId(id, function (err, res) {
        if (res == null) {
            return false;
        }
        robotModel.set('gameId', gameId);
        robotModel.save(function (err1, res1) {
        });
    });
};
gameUser.escapee = function (id, cid, uuid, downGold) {
    var userModel = new userDao();
    userModel.inId(id, function (err, res) {
        if (res == null) {
            return false;
        }
        if (downGold != 0) {
            var gold = res.res.gold - downGold;
            userModel.set('gold', gold);
        }
        userModel.set('gameId', 0);
        userModel.save(function (err1, res1) {
            if (res1 == null) {
                return false;
            }
            if (cid && uuid) {
                if (downGold != 0) {
                    msg.send(cid, uuid, 'globalUserChange', {'gold': {'add': -downGold, 'now': gold}});
                }
                msg.send(cid, uuid, 'gameIdClear');
            }
        });
    });
};
gameUser.robotEscapee = function (id, downGold) {
    var robotModel = new robotDao();
    robotModel.inId(id, function (err, res) {
        if (res == null) {
            return false;
        }
        if (downGold != 0) {
            var gold = res.res.gold - downGold;
            robotModel.set('gold', gold);
        }
        robotModel.set('gameId', 0);
        robotModel.save(function (err1, res1) {
        });
    });
};
gameUser.saveResult = function (id, cid, uuid, addCoin, coinType, winner) {
    var userModel = new userDao();
    userModel.inId(id, function (err, res) {
        if (res == null) {
            return false;
        }
        var resultData = {};
        userModel.set('gameId', 0);
        if (addCoin) {
            var coin = res.res[coinType] + addCoin;
            userModel.set(coinType, coin);
            resultData[coinType] = {'now': coin, 'add': addCoin};
        }
        var times = res.res.playTimes + 1;
        userModel.set('playTimes', times);
        resultData.playTimes = {'now': times, 'add': 1};
        if (winner) {
            var victory = res.res.playVictory + 1;
            var wins = res.res.playWins + 1;
            userModel.set('playVictory', victory);
            userModel.set('playWins', wins);
            resultData.playVictory = {'now': victory, 'add': 1};
            resultData.playWins = {'now': wins, 'add': 1};
        } else if (res.res.playWins > 0) {
            var add = -res.res.playWins;
            userModel.set('playWins', 0);
            resultData.playWins = {'now': 0, 'add': add};
        }
        userModel.save(function (err, res) {
            if (res == null) {
                return false;
            }
            msg.send(cid, uuid, 'globalUserChange', resultData);
            msg.send(cid, uuid, 'gameIdClear');
        });
    });
};
gameUser.robotResult = function (id, addCoin, coinType) {
    var robotModel = new robotDao();
    robotModel.inId(id, function (err, res) {
        if (res == null) {
            return false;
        }
        robotModel.set('gameId', 0);
        if (coinType == 'gold') {
            var coin = res.res.gold + addCoin;
            robotModel.set('gold', coin);
        }
        robotModel.save(function (err, res) {
        });
    });
};
